package com.cug.smartranch.animal.admin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cug.smartranch.animal.admin.dao.entity.Animal;
import com.cug.smartranch.animal.admin.dao.entity.FeedDetail;
import com.cug.smartranch.animal.admin.dao.mapper.AnimalMapper;
import com.cug.smartranch.animal.admin.dao.mapper.FeedDetailMapper;
import com.cug.smartranch.animal.admin.dto.req.FeedDetailPageQueryReqDTO;
import com.cug.smartranch.animal.admin.dto.resp.FeedDetailPageQueryRespDTO;
import com.cug.smartranch.animal.admin.service.FeedDetailService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 喂养详情记录业务逻辑实现层
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class FeedDetailServiceImpl extends ServiceImpl<FeedDetailMapper, FeedDetail> implements FeedDetailService {
    private final FeedDetailMapper feedDetailMapper;
    /**
     * 分页查询喂养详情信息
     * @param requestParam
     * @return
     */
    @Override
    public IPage<FeedDetailPageQueryRespDTO> selectAllFeedDetail(FeedDetailPageQueryReqDTO requestParam) {
        // 构建分页参数
        Page<FeedDetail> page = new Page<>(requestParam.getPageNo(), requestParam.getPageSize());
        // 构建查询参数
        LambdaQueryWrapper<FeedDetail> queryWrapper = Wrappers.lambdaQuery(FeedDetail.class);
        queryWrapper.like(StrUtil.isNotBlank(requestParam.getEquipmentName()), FeedDetail::getEquipmentName, requestParam.getEquipmentName());
        queryWrapper.eq(Objects.nonNull(requestParam.getAnimalGroup()),FeedDetail::getAnimalGroup, requestParam.getAnimalGroup());
        queryWrapper.eq(Objects.nonNull(requestParam.getFeedCategory()),FeedDetail::getFeedCategory, requestParam.getFeedCategory());
        // 查询分页
        Page<FeedDetail> feedDetailPage = feedDetailMapper.selectPage(page, queryWrapper);
        // 结果转换
        IPage<FeedDetailPageQueryRespDTO> result = feedDetailPage.convert(detail -> {
            FeedDetailPageQueryRespDTO respDTO = new FeedDetailPageQueryRespDTO();
            respDTO.setFeedAmount(detail.getFeedAmount());
            respDTO.setFeedCategory(detail.getFeedCategory());
            respDTO.setFeedTime(detail.getFeedTime());
            respDTO.setAnimalGroup(detail.getAnimalGroup());
            respDTO.setEquipmentName(detail.getEquipmentName());
            return respDTO;
        });
        return result;
    }
}
